<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>合同列表</title>
    <link rel="shortcut icon" href="<c:url value="/easyui/css/images/favicon.ico" />"/>
    <link rel="stylesheet" href="<c:url value="/css/flipclock.css"/>"/>
    <link href="<c:url value="/css/CalcSS3.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/css/index.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/easyui/css/easyui.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/easyui/css/easyui_animation.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/easyui/css/easyui_plus.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/easyui/css/insdep_theme_default.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/easyui/css/icon.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/easyui/css/font-awesome-4.7.0/css/font-awesome.min.css"/>" rel="stylesheet"
          type="text/css"/>


    <!-- 引入 ECharts 文件 -->
    <script src="<c:url value="/js/echarts.js"/>"></script>
    <script src="/js/saling/fixAccuracyLost.js"></script>
    <!--放入自定义的图表-->
    <script src="<c:url value="/js/monitor.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/easyui/js/jquery.min.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/easyui/js/jquery.easyui.min.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/easyui/js/jquery.insdep-extend.min.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/js/utils/DateFormate.js"/>"></script>

    <link href="<c:url value=" /css/utils/utilsDatagrid.css"/>" rel="stylesheet" type="text/css"/>
    <link href="<c:url value="/css/utils/utilsForm.css"/>" rel="stylesheet" type="text/css"/>
</head>
<body>

<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center'" style="padding: 5px">
        <table id="dr5hx-contractList" ,class="easyui-datagrid table-chance" style="width: auto" data-options="fit:true,striped:true,height:350,fitColumns:true,checkOnSelect:false,
   autoRowHeight:false,nowrap:true,url:'/saling/getContractList',method:'get',pagination:true,toolbar:'#contract-tools',
pageList:[10,20,30,40],pageSize:10">
            <thead>
            <tr class="datagrid-header-row">
                <th data-options="field:'code',width:10,align:'center',checkbox:true">编码</th>
                <th data-options="field:'contract',width:50,align:'center',
                formatter:function(value,row,index){
					return row.contract.contractId}">ID
                </th>
                <th data-options="field:'topic',width:120,align:'center',formatter:function(value,row,index){
					return row.contract.contractTopic}">主题
                </th>
                <th data-options="field:'cname',width:100,align:'center'">对应客户</th>
                <th data-options="field:'contractNumber',width:130,align:'center',formatter:function(value,row,index){
                    return row.contract.contractNumber;

                }">合同订单号
                </th>
                <th data-options="field:'contractMoney',width:60,align:'center',formatter:function(value,row,index){
                    return row.contract.contractMoney;
                }">总金额￥
                </th>
                <th data-options="field:'orderBackMoney', width:60,align:'center',formatter:function(value,row,index){
                    return row.contract.orderBackMoney;
                }">回款金额￥
                </th>
                <th data-options="field:'contractState',width:40,align:'center',formatter:function(value,row,index){
                    return row.contract.contractState;
                }">状态
                </th>
                <th data-options="field:'eName',width:60,align:'center'">所有者</th>
                <th data-options="field:'orderStartTime',width:50,align:'center',formatter:function(value,row,index){
                var a= new Date(row.contract.orderStartTime);
					return a.format('yyyy-MM-dd');
                }">开始时间
                </th>
                <th data-options="field:'operation',width:100,align:'center'" formatter="format">操作</th>
            </tr>
            </thead>
            <tbody>


            </tbody>
        </table>
    </div>
</div>

<div id="advance-contract-chance-dialog" class="easyui-dialog" style="background-color:#EBEFF2;" data-options="title:'高级查询',modal:true,
			iconCls:'icon-save2',
			width:'100%',
			height:'100%',
			closed:true,
			href:'/toContractAdvancedSearch',
			bodyCls:'by',
			shadow:true"></div>
<!--工具栏-->
<div id="contract-tools">
    <a id="dr5hx-contract-add" class="easyui-linkbutton" data-options="iconCls:'icon-mini_add2',plain:true">新建</a>
    <a id="dr5hx-contract-delete" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-my_delete',plain:true">删除</a>

    <a class="easyui-linkbutton" data-options="iconCls:'icon-my_search',plain:true,onClick:function(){
        $('#dr5hx-contractList').datagrid({
            url:'/saling/getContractList',
            method:'get'
        })
    }">查询所有</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-my_search',plain:true,onClick:function(){
    $('#advance-contract-chance-dialog').dialog('open')

    }">高级查询</a>


</div>
<div id="dr5hx-contract-edit" class="easyui-dialog" style="background-color:#EBEFF2;" data-options="iconCls:'icon-save2',
			width:900,
			height:750,
			closed:true,
			href:'/saling/toLoadContract',
			method:'post',
			bodyCls:'by',
			shadow:true,
			modal:true ,
            buttons:'#dr5hx-contract-edit-buttons'
">


</div>
<div id="dr5hx-contract-edit-buttons">
    <a href="#" id="dr5hx-contract-edit-save" class="easyui-linkbutton"
       data-options="iconCls:'icon-ok2'">保存</a>
    <a href="#" id="dr5hx-contract-edit-close" class="easyui-linkbutton" data-options="iconCls:'icon-cancel2',onClick:function(){
    $('#dr5hx-contract-edit').window('close')
    }">关闭</a>
</div>
<div id="dr5hx-contract-planingback-dialog" class="easyui-dialog" style="background-color:#EBEFF2;"
     data-options="iconCls:'icon-save2',
			width:680,
			height:'auto',
			bodyCls:'by',
			shadow:true,
			closed:true,
			modal:true,
			title:'回款计划',
			buttons:'#dr5hx-contract-planingback-dialog-buttons'
">
    <form id="dr5hx-contract-planingback-dialog-form" method="post">
        <table align="center" id="dr5hx-contract-planingback-dialog-form-table"
               data-options="align:'center',
                       width:'auto',
                        toolbar:'#dr5hx-contract-planingback-dialog-form-table_buttons'"
               class="easyui-datagrid"
               style="min-height:100px">
            <thead>
            <tr class="datagrid-header-row">
                <th data-options="field:'planingbacktime',width:200,align:'center'">期数
                </th>
                <th data-options="field:'planingbackmoney',width:200,align:'center'">回款金额
                </th>
                <th data-options="field:'planingbackdate',width:200,align:'center'">预计回款时间
                </th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>

    </form>
</div>
<div id="dr5hx-contract-planingback-dialog-form-table_buttons">
    <a class="easyui-linkbutton" id="dr5hx-contract-planingback-dialog-form-table_buttons-add" plain="true"
       iconCls="icon-mini_add2">增加</a>
    <a class="easyui-linkbutton" id="dr5hx-contract-planingback-dialog-form-table_buttons-delete" plain="true"
       iconCls="icon-my_delete">删除</a>
</div>
<div id="dr5hx-contract-planingback-dialog-form-table-dialog" class="easyui-dialog" data-options="iconCls:'icon-save2',
width:600,
height:'220',
bodyCls:'by',
shadow:true,
closed:true,
modal:true,
title:' 添加回款计划',
buttons:'#dr5hx-contract-planingback-dialog-form-table-dialog-buttons'">
    <br/>
    <br/>
    <span style="display: inline-block;width: 20px;"></span>
    <input id="dr5hx-contract-planingback-dialog-form-table-dialog-times" class="easyui-textbox"
           data-options="label:'期次',width:250,labelWidth:50"
           validType="checkNumber" required="required">
    <span style="display: inline-block;width: 40px;"></span>
    <input id="dr5hx-contract-planingback-dialog-form-table-dialog-money" class="easyui-textbox"
           data-options="label:'回款金额',width:250,labelWidth:70"
           validType="checkDotNumber" required="required">
    <br/>
    <br/>
    <span style="display: inline-block;width: 20px;"></span>
    <input id="dr5hx-contract-planingback-dialog-form-table-dialog-planingbackdate"
           class="easyui-datebox"
           data-options="label:'预计回款日期',width:250,labelWidth:100,editable:false" required="required">
</div>
<div id="dr5hx-contract-planingback-dialog-form-table-dialog-buttons">
    <a href="#" id="dr5hx-contract-planingback-dialog-form-table-dialog-button-save" class="easyui-linkbutton"
       data-options="iconCls:'icon-ok2'">保存</a>
    <a href="#" id="dr5hx-contract-planingback-dialog-form-table-dialog-close" class="easyui-linkbutton" data-options="iconCls:'icon-cancel2',onClick:function(){
    $('#dr5hx-contract-planingback-dialog-form-table-dialog').window('close')
    }">关闭</a>
</div>
<div id="dr5hx-contract-planingback-dialog-buttons">
    <a href="#" id="dr5hx-contract-planingback-dialog-button-save" class="easyui-linkbutton"
       data-options="iconCls:'icon-ok2'">保存</a>
    <a href="#" id="dr5hx-contract-planingback-dialog-button-close" class="easyui-linkbutton" data-options="iconCls:'icon-cancel2',onClick:function(){
    $('#dr5hx-contract-planingback-dialog').window('close')
    }">关闭</a>
</div>
<script type="text/javascript">
    $("#dr5hx-contract-planingback-dialog-button-save").linkbutton({
        onClick: function () {
            var rows = $("#dr5hx-contract-planingback-dialog-form-table").datagrid("getRows")
            var contractId = $("#dr5hx-contract-planingback-dialog").dialog("options").contractId
            $("#dr5hx-contract-planingback-dialog-form").form(
                {
                    url: "/savePlaningBackListByContractId",
                    onSubmit: function (params) {
                        params.rows = JSON.stringify(rows)
                        params.contractId = contractId
                    }, success: function (data) {
                        data = eval("(" + data + ")")
                        if (data.success == true) {
                            $("#dr5hx-contract-planingback-dialog").dialog('close')
                            $("#dr5hx-contract-planingback-dialog").dialog('reload')
                        }
                    }
                }
            ).submit()
        }
    })
    $("#dr5hx-contract-planingback-dialog-form-table-dialog-button-save").linkbutton({
        onClick: function () {
            var times = $("#dr5hx-contract-planingback-dialog-form-table-dialog-times").textbox('getValue')
            var money = $("#dr5hx-contract-planingback-dialog-form-table-dialog-money").textbox('getValue')
            var date = $("#dr5hx-contract-planingback-dialog-form-table-dialog-planingbackdate").datebox('getValue')
            var patrn = /^\d+$/;
            var pattern = /^\d+([.]\d+)*$/
            var checkMoney = $("#dr5hx-contract-planingback-dialog-form-table-dialog").dialog("options").money
            if (patrn.test(times) && pattern.test(money) && checkMoney >= money && date) {
                $("#dr5hx-contract-planingback-dialog-form-table").datagrid("appendRow", {
                    'planingbacktime': times,
                    'planingbackmoney': money,
                    'planingbackdate': date
                })
                $("#dr5hx-contract-planingback-dialog-form-table-dialog").dialog('close')
            } else {
                $.messager.alert("提示", "不能为空,或者金额不能超过预设金额")
            }
        }
    })

    $("#dr5hx-contract-planingback-dialog-form-table_buttons-add").linkbutton({
        onClick: function () {
            var rows = $("#dr5hx-contract-planingback-dialog-form-table").datagrid("getRows")
            var money = $("#dr5hx-contract-planingback-dialog").dialog('options').money
            if (rows.length == 0) {
                $("#dr5hx-contract-planingback-dialog-form-table-dialog").dialog({"money": money});

                $("#dr5hx-contract-planingback-dialog-form-table-dialog-money").textbox({
                    value: money
                })
                $("#dr5hx-contract-planingback-dialog-form-table-dialog").dialog('open');
            } else {
                var planingbackmoney = 0;
                for (var i = 0; i < rows.length; i++) {
                    planingbackmoney = planingbackmoney.add(rows[i].planingbackmoney)
                }
                if (money.sub(planingbackmoney) > 0) {
                    $("#dr5hx-contract-planingback-dialog-form-table-dialog-money").textbox({
                        value: money.sub(planingbackmoney)
                    })
                    $("#dr5hx-contract-planingback-dialog-form-table-dialog").dialog({"money": money - planingbackmoney});
                    $("#dr5hx-contract-planingback-dialog-form-table-dialog").dialog('open');
                } else {
                    $.messager.alert("提示", "回款金额足够")
                }
            }
        }
    })
    $("#dr5hx-contract-add").linkbutton({
        onClick: function () {
            $('#dr5hx-contract-edit').dialog({
                href: '/saling/toAddContract',
                title: '添加合同/订单',
            });
            $('#dr5hx-contract-edit').dialog({
                onLoad: function () {
                    $("#dr5hx-contract-load-add-customer").combobox({
                        url: '/saling/getClientInfoList',
                        method: 'post',
                        valueField: 'cid',
                        textField: 'cname',
                        panelHeight: 'auto',
                        onSelect: function (record) {
                            var url = "/saling/loadContactInformation";
                            $("#dr5hx-contract-load-add-contact").combobox({
                                url: url,
                                method: 'post',
                                queryParams: {'cid': record.cid},
                                valueField: 'contactid',
                                textField: 'contactName'
                            });
                            $("#dr5hx-contract-load-add-plan").combobox({
                                url: '/saling/getCustomerPlan',
                                method: 'post',
                                queryParams: {'cid': record.cid},
                                valueField: 'planId',
                                textField: 'planName'
                            });
                        }
                    });
                    $("#dr5hx-contract-load-add-emp").combobox({
                        url: '/saling/getUserInfoList',
                        method: 'post',
                        valueField: 'uId',
                        textField: 'uname'
                    });
                }
            });
            $("#dr5hx-contract-edit-save").linkbutton({
                onClick: function () {
                    $('#dr5hx-contract-load-add').form({
                        url: '/saling/uploadContractInformation',
                        onSubmit: function () {
                            return $(this).form('validate');
                        },
                        success: function (data) {
                            $('#dr5hx-contract-edit').window('close')
                            $.messager.alert('提示', data);
                            $('#dr5hx-contractList').datagrid('reload')


                        }
                    }).submit();
                }
            })
            $('#dr5hx-contract-edit').dialog('open');
        },

    })
    ;

    //多行删除事件
    $("#dr5hx-contract-delete").linkbutton({
        onClick: function () {
            $.messager.confirm('警告', '是否要删除', function (v) {
                if (v) {
                    var row = $('#dr5hx-contractList').datagrid('getChecked');
                    if (row.length == 0) {
                        $.messager.alert("提示", "请先选中要删除的行");
                    } else {
                        for (var i = 0; i < row.length; i++) {
                            if (row[i]) {
                                $.post('/saling/deleteContractById', {contractId: row[i].contract.contractId}, function (r) {
                                    if (r.success) {
                                        $('#dr5hx-contractList').datagrid('reload');
                                    } else {
                                        $.messager.alert("提示", r.message);
                                    }
                                });
                            }
                        }
                    }
                }

            });
        }
    });
    $.extend($.fn.validatebox.defaults.rules, {
        checkDotNumber: {
            validator: function (value) {
                var pattern = /^\d+([.]\d+)*$/
                return pattern.test(value);
            },
            message: '必须为数字或小数'
        }
    });
    $.extend($.fn.validatebox.defaults.rules, {
        checkNumber: {
            validator: function (value) {
                var patrn = /^\d+$/;
                return patrn.test(value);
            },
            message: '必须为数字'
        }
    });

    $(function () {
        //修改事件处理
        $("#dr5hx-contractList").datagrid({
            onLoadSuccess: function (data) {
                $("a[name='edit']").linkbutton({
                    width: 40, iconCls: 'icon-my_edit', plain: true, onClick: function () {
                        var rows = $('#dr5hx-contractList').datagrid('getRows');
                        $('#dr5hx-contractList').datagrid({
                            onClickCell: function (index, field, value) {
                                var contractId = rows[index].contract.contractId;
                                $.post('/saling/loadOneContractVO', {id: contractId}, function (r) {
                                    if (r) {
                                        $('#dr5hx-contract-edit').dialog({
                                            title: '修改合同/订单',
                                            href: '/saling/toLoadContract'
                                        });
                                        $('#dr5hx-contract-edit').dialog({'contract': r});
                                        $('#dr5hx-contract-edit').dialog({
                                            onLoad: function () {
                                                var r = $('#dr5hx-contract-edit').dialog('options').contract;
                                                $('#dr5hx-contract-load-edit-dialog').dialog({'contractId': r.contract.contractId});
                                                $("#dr5hx-contract-load-edit-customer").textbox({value: r.cname});
                                                $("#dr5hx-contract-load-edit-topic").textbox({value: r.contract.contractTopic});
                                                $("#dr5hx-contract-load-edit-money").textbox({value: r.contract.contractMoney});
                                                $("#dr5hx-contract-load-edit-backmoney").textbox({value: r.contract.orderBackMoney});
                                                $('#dr5hx-contract-load-edit-contractId').val(r.contract.contractId)
                                                $("#dr5hx-contract-load-edit-startdate").datebox({
                                                    value: new Date(r.contract.orderStartTime).format('yyyy-MM-dd')
                                                });
                                                $("#dr5hx-contract-load-edit-overdate").datebox({
                                                    value: new Date(r.contract.orderOverTime).format('yyyy-MM-dd')
                                                });

                                                $("#dr5hx-contract-load-edit-contact").combobox({
                                                    value: r.contract.contractCustomer,
                                                    url: '/saling/loadContactInformation',
                                                    method: 'post',
                                                    queryParams: {'cid': r.contract.fkCustomerId},
                                                    valueField: 'contactid',
                                                    textField: 'contactName'
                                                });
                                                $("#dr5hx-contract-load-edit-emp").combobox({
                                                    value: r.eName,
                                                    url: '/saling/getUserInfoList',
                                                    method: 'post',
                                                    valueField: 'uId',
                                                    textField: 'uname'
                                                });
                                                $("#dr5hx-contract-load-edit-contractnumber").textbox({value: r.contract.contractNumber});
                                                $("#dr5hx-contract-load-edit-state").combobox({value: r.contract.contractState});
                                                $("#dr5hx-contract-load-edit-plan").combobox({
                                                    value: r.planName,
                                                    url: '/saling/getCustomerPlan',
                                                    method: 'post',
                                                    queryParams: {'cid': r.contract.fkCustomerId},
                                                    valueField: 'planId',
                                                    textField: 'planName'
                                                });
                                                $("#dr5hx-contract-load-edit-isrefund").textbox({value: r.contract.contractIsRefund});
                                                $("#dr5hx-contract-load-edit-remark").textbox({value: r.contract.contractRemark});
                                                $("#dr5hx-contract-load-edit-address").textbox({value: r.contract.contractAddress});
                                                $("#dr5hx-contract-load-edit-codetails").datagrid({
                                                    striped: true,
                                                    fitColumns: true,
                                                    autoRowHeight: true,
                                                    nowrap: false,
                                                    url: '/saling/loadCODetailsList',
                                                    queryParams: {
                                                        contractId: r.contract.contractId
                                                    },
                                                    method: 'post',
                                                });
                                                $("#dr5hx-addCODetailsForm-contractId").val(r.contract.contractId);

                                            }
                                        });
                                        $('#dr5hx-contract-load-edit-dialog').window('destroy')
                                        $('#dr5hx-contract-edit').dialog("open");

                                    } else {
                                        $.messager.alert("ERROR", "NO MESSAGER RECEIVERD");
                                    }
                                    $('#dr5hx-contractList').datagrid({
                                        onClickCell: function (index) {
                                        }
                                    });
                                });

                            }


                        });

                    }
                });
                $("#dr5hx-contract-edit-save").linkbutton({
                    onClick: function () {
                        $('#dr5hx-contract-load-edit').form({
                            url: '/saling/saveEditContract',
                            onSubmit: function () {
                                var isValid = $(this).form('validate');
                                return isValid;
                            },
                            success: function (data) {
                                $('#dr5hx-contract-edit').window('close')
                                $('#dr5hx-contractList').datagrid('reload')

                            }
                        }).submit();
                        var contract = $('#dr5hx-contract-edit').dialog('options').contract.contract.contractId
                        $.post("/getContractMoney", {
                            contractId: contract
                        }, function (result) {
                            if (result.value == false) {
                                $.messager.confirm("提示", "检测到回款计划金额与合同金额不一致，正在打开回款计划。。", function (v) {
                                    $("#dr5hx-contract-planingback-dialog").dialog({"money": result.one - result.two})
                                    $("#dr5hx-contract-planingback-dialog").dialog({"contractId": data.rows[0].contract.contractId})
                                    $("#dr5hx-contract-planingback-dialog").window('open')
                                })
                            }
                        })
                    }
                })
                // 添加细节处理事件
                $("a[name='details']").linkbutton({
                    width: 40, iconCls: 'icon-my_detail', plain: true,
                    onClick: function () {
                        var rows = $('#dr5hx-contractList').datagrid('getRows');
                        $('#dr5hx-contractList').datagrid({
                            onClickCell: function (index, field, value) {
                                var contractId = rows[index].contract.contractId;
                                $(".dr5hx-contractList-detail").attr("href", "/saling/details?id=" + contractId);

                                $('#dr5hx-contractList').datagrid({
                                    onClickCell: function (index) {
                                    }
                                });
                            }
                        });


                    }
                });
                // 添加删除方法事件
                $("a[name='delete']").linkbutton({
                    width: 40, iconCls: 'icon-my_delete', plain: true,
                    onClick: function () {
                        $('#dr5hx-contractList').datagrid({
                            onClickCell: function (index) {
                                var rows = $('#dr5hx-contractList').datagrid('getRows');

                                $.post('/saling/deleteContractById', {contractId: rows[index].contract.contractId}, function (r) {

                                    if (r.success) {
                                        $('#dr5hx-contractList').datagrid('reload');
                                    } else {
                                        $.messager.alert("提示", r.message);
                                    }

                                });
                                $('#dr5hx-contractList').datagrid({
                                    onClickCell: function (index) {
                                    }
                                });
                            }
                        });
                    }
                });


            }
        });


    });

    <%-- 添加到操作栏方法--%>

    function format(value, row, index) {
        var str = '<a name="edit" class="dr5hx-contract-edit"></a>' +
            '<a class="dr5hx-contractList-detail" href="#" target="_blank" name="details"  "></a>' + '<a name="delete"></a>';
        return str;
    }
</script>
</body>
</html>
